﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

namespace demo3{
	public class PlayerController : NetworkBehaviour {

		public GameObject shootPrefab;
		public Transform shootPos;

		
		// Update is called once per frame
		void Update () {
			if (!isLocalPlayer)
				return;
			float h = Input.GetAxis ("Horizontal");
			float v = Input.GetAxis ("Vertical");
			transform.Rotate (Vector3.up * h * 240 * Time.deltaTime);
			transform.Translate (Vector3.forward * v * Time.deltaTime * 5);
			if (Input.GetKeyDown (KeyCode.Space)) {
				CmdShot ();
			}
		}


		public override void OnStartLocalPlayer ()
		{
			transform.FindChild ("Body").GetComponent<MeshRenderer> ().material.color = Color.blue;
		}

		[Command]
		void CmdShot(){
			GameObject bullet = GameObject.Instantiate (shootPrefab,shootPos.position,shootPos.rotation) as GameObject;

			bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 10;
			bullet.transform.SetParent (GameObject.Find ("Bullets").transform);
			Destroy (bullet, 3);
			NetworkServer.Spawn (bullet);
		}
	}
}
